package com.weiyuwqj.app.mobile.account;

import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.model.BasUserSecurity;
import com.weiyuwqj.api.model.BusAccountBank;
import com.weiyuwqj.api.service.*;
import com.weiyuwqj.api.util.HttpUtils;
import com.weiyuwqj.api.util.JsonUtils;
import com.weiyuwqj.api.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.*;

/**
 * Created by yxw on 2016/1/8.
 * 银行卡------>绑定银行卡
 */
@Controller
@RequestMapping(value="/mobile/account")
public class MobileAccountBankController {

    @Autowired
    private BusAccountBankService accountBankService;

    /**
     * 显示银行卡
     * @param userId 用户ID
     * @return 银行卡信息
     */
    @ResponseBody
    @RequestMapping(value="/getBankByUserId", method= RequestMethod.POST)
    public Object bankListByUserId(String userId){
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("status",1); // 启用
        BusAccountBank bank=accountBankService.getOneByUserIdAndStatus(map);
        Map param=new HashMap();
        if(null!=bank){
            bank.setCardNo(StringUtils.getString(bank.getCardNo()));
            param.put("result","success");
            param.put("bank",bank);
            return param;
        }else {
            return Message.FAIL;
        }
    }

    /**
     * 判断实名认证
     */
    @ResponseBody
    @RequestMapping(value="/realNameByUserId", method=RequestMethod.POST)
    public Object realNameAuthentication(Integer userId){
        Map map = new HashMap();
        map.put("userId", userId);
        String response = HttpUtils.URLPost("/admin/userSecurity/getOne", map);
        BasUserSecurity basUserSecurity = JsonUtils.json2Object(response, BasUserSecurity.class);
        if(basUserSecurity == null || basUserSecurity.getRealnameStatus() == 0){
            return new ModelAndView(new RedirectView("/user/security?flag=7"));     //没有实名认证则跳转至实名认证页面
        }
        return "";  //实名认证页面
    }


    /**
     * 更改银行卡
     * @param newCardNo   新卡号
     * @param mobile      预留手机号码
     * @param bankName    银行名称
     * @param userId      用户id
     * @return 更改结果
     */
    @ResponseBody
    @RequestMapping(value = "/changeCard")
    public Object changeCard(String newCardNo,String mobile,String bankName,Integer userId){
        try {
            accountBankService.changeCard(newCardNo, mobile, bankName, userId);
        } catch (Exception e) {
            return Message.FAIL;
        }
        return Message.OK;
    }
}
